/**
 * @Author：ming
 * @Date：2023/10/1 3:57 PM
 * @Filename：Lc61
 */

/**
 * @author ming
 */
public class Lc61 {
    public ListNode rotateRight(ListNode head, int k) {
        if(head==null||head.next==null){
            return head;
        }
        //遍历获取链表长度
        int len = 1;
        ListNode q = head;
        while (q.next!=null){
            len++;
            q=q.next;
        }
        //k对len取余，当k大于len时不用回头再旋转
        int right = k%len;
        //向右移动right，相当于向左移动len-right
        int left = len -right;
        //连接成环
        q.next = head;

        int count = 0;
        //遍历找到新的头节点的前一个结点q
        while (count<left){
            q = q.next;
            count++;
        }

        ListNode p = q.next;
        //断开环
        q.next =null;
        return p;


    }

}
